package bb.lanxing.activity.sport;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.adapter.BaseListAdapter;
import bb.lanxing.util.CommonUtil;

public class BDOfflineCitiesActivity extends BaseActivity implements MKOfflineMapListener {
    private ListView cityListView;
    private MKOfflineMap offline;
    private BDOfflineCityAdapter offlineCityAdapter;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.city_list);
        setupActionBar(true, -1, true);
        Intent intent = getIntent();
        int intExtra = intent.getIntExtra("id", 0);
        setTitle(intent.getStringExtra("name"));
        this.cityListView = findViewById(R.id.listView);
        this.offline = BDOfflineManageActivity.offline;
        init(getParent(intExtra));
    }

    @Override
    public void onResume() {
        super.onResume();
        this.offline.init(this);
    }

    @Override
    public void onGetOfflineMapState(int i, int i2) {
        MKOLUpdateElement updateInfo;
        if (i != 0 || (updateInfo = this.offline.getUpdateInfo(i2)) == null) {
            return;
        }
        this.offlineCityAdapter.notifyDataSetChanged();
        if ((updateInfo.status != 1 && updateInfo.status != 10) || updateInfo.ratio != 100) {
            return;
        }
        this.cityListView.postDelayed(() -> BDOfflineCitiesActivity.this.offlineCityAdapter.notifyDataSetChanged(), 100L);
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        MKOLUpdateElement updateInfo = this.offline.getUpdateInfo(this.offlineCityAdapter.getItem(((AdapterView.AdapterContextMenuInfo) contextMenuInfo).position).cityID);
        if (updateInfo != null && updateInfo.update && updateInfo.status == 4) {
            contextMenu.add(0, 1, 0, getString(R.string.update));
        } else if (updateInfo == null || updateInfo.status == 3) {
            contextMenu.add(0, 2, 0, getString(R.string.map_offline_btn_download));
        }
        if (updateInfo != null && (updateInfo.status == 1 || updateInfo.status == 2)) {
            contextMenu.add(0, 3, 0, getString(R.string.map_offline_btn_pause));
        }
        if (updateInfo != null && (updateInfo.status == 3 || updateInfo.status == 4 || updateInfo.status == 10)) {
            contextMenu.add(0, 4, 0, getString(R.string.delete));
        }
        super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem menuItem) {
        MKOLSearchRecord item = this.offlineCityAdapter.getItem(((AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo()).position);
        int itemId = menuItem.getItemId();
        if (itemId == 1 || itemId == 2) {
            this.offline.start(item.cityID);
            setResult(-1);
        } else if (itemId == 3) {
            MKOLUpdateElement updateInfo = this.offline.getUpdateInfo(item.cityID);
            if (updateInfo != null && (updateInfo.status == 1 || updateInfo.status == 2)) {
                this.offline.pause(updateInfo.cityID);
                this.offlineCityAdapter.notifyDataSetChanged();
            }
        } else if (itemId == 4) {
            this.offline.remove(item.cityID);
            this.offlineCityAdapter.notifyDataSetChanged();
            setResult(-1);
        }
        return super.onContextItemSelected(menuItem);
    }

    private void init(MKOLSearchRecord mKOLSearchRecord) {
        BDOfflineCityAdapter bDOfflineCityAdapter = new BDOfflineCityAdapter(this, mKOLSearchRecord.childCities);
        this.offlineCityAdapter = bDOfflineCityAdapter;
        bDOfflineCityAdapter.insert(0, mKOLSearchRecord);
        this.cityListView.setAdapter(this.offlineCityAdapter);
        this.cityListView.setOnItemClickListener((adapterView, view, i, j) -> {
            BDOfflineCitiesActivity bDOfflineCitiesActivity = BDOfflineCitiesActivity.this;
            bDOfflineCitiesActivity.registerForContextMenu(bDOfflineCitiesActivity.cityListView);
            adapterView.showContextMenuForChild(view);
            BDOfflineCitiesActivity bDOfflineCitiesActivity2 = BDOfflineCitiesActivity.this;
            bDOfflineCitiesActivity2.unregisterForContextMenu(bDOfflineCitiesActivity2.cityListView);
        });
    }

    private MKOLSearchRecord getParent(int i) {
        ArrayList<MKOLSearchRecord> offlineCityList = this.offline.getOfflineCityList();
        if (offlineCityList != null) {
            Iterator<MKOLSearchRecord> it = offlineCityList.iterator();
            while (it.hasNext()) {
                MKOLSearchRecord next = it.next();
                if (next.cityID == i) {
                    return next;
                }
            }
            return null;
        }
        return null;
    }

    public class BDOfflineCityAdapter extends BaseListAdapter<MKOLSearchRecord> {
        private String[] statusTypes;

        private BDOfflineCityAdapter(Context context, List<MKOLSearchRecord> list) {
            super(context, list);
            this.statusTypes = this.mContext.getResources().getStringArray(R.array.map_offline_state);
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            String str;
            if (view == null) {
                view = LayoutInflater.from(this.mContext).inflate(R.layout.city_item, viewGroup, false);
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            MKOLSearchRecord item = getItem(i);
            viewHolder.name.setText(item.cityName);
            viewHolder.size.setText(CommonUtil.getSizeString(item.dataSize));
            MKOLUpdateElement updateInfo = BDOfflineCitiesActivity.this.offline.getUpdateInfo(item.cityID);
            if (updateInfo != null) {
                if (viewHolder.state.getVisibility() != View.VISIBLE) {
                    viewHolder.state.setVisibility(View.VISIBLE);
                }
                if (updateInfo.status == 1) {
                    str = this.statusTypes[0] + " " + updateInfo.ratio + " %";
                } else if (updateInfo.status == 2) {
                    str = this.statusTypes[1];
                } else if (updateInfo.status == 4 || updateInfo.status == 10) {
                    str = this.statusTypes[2];
                } else if (updateInfo.status == 3) {
                    str = this.statusTypes[3];
                } else {
                    str = updateInfo.update ? this.mContext.getString(R.string.map_offline_city_has_update) : "";
                }
                viewHolder.state.setText(str);
            } else if (viewHolder.state.getVisibility() == View.VISIBLE) {
                viewHolder.state.setVisibility(View.GONE);
            }
            return view;
        }

        class ViewHolder {
            private TextView name;
            private TextView size;
            private TextView state;

            private void bindView(View view) {
                this.name = view.findViewById(R.id.item);
                this.size = view.findViewById(R.id.size);
                this.state = view.findViewById(R.id.state);
            }

            public ViewHolder(View view) {
                bindView(view);
            }
        }
    }
}